﻿Public Class frmCaseIncome

    Private Sub frmCaseIncome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getComboCase()
        getComboContract()
        reset()
    End Sub

    Protected Sub getComboCase()
        Dim oc As New cControl
        oc.getCombo(cboCase, "SELECT CaseID,CaseNo FROM tblCase", "CaseNo", "CaseID")
    End Sub

    Protected Sub getComboContract()
        Dim oc As New cControl
        oc.getCombo(cboContract, "SELECT ContractID,CompanyName FROM tblCompany", "CompanyName", "ContractID")
    End Sub

    Protected Sub reset()
        txtDesc.Focus()
        rdCase.Checked = False
        rdContract.Checked = False
        rdOther.Checked = True
        cboCase.SelectedIndex = -1
        cboContract.SelectedIndex = -1

        txtDesc.Clear()
        txtRevenue.Clear()
        txtExpense.Clear()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub rdCase_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdCase.CheckedChanged
        RadioCheckedChanged()
    End Sub

    Private Sub rdContract_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContract.CheckedChanged
        RadioCheckedChanged()
    End Sub

    Private Sub rdOther_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdOther.CheckedChanged
        RadioCheckedChanged()
    End Sub

    Protected Sub RadioCheckedChanged()
        If rdCase.Checked Then
            btnFindCase.Enabled = True
            btnFindContract.Enabled = False
            cboContract.SelectedIndex = -1
        ElseIf rdContract.Checked Then
            btnFindCase.Enabled = False
            btnFindContract.Enabled = True
            cboCase.SelectedIndex = -1
        Else
            btnFindCase.Enabled = False
            btnFindContract.Enabled = False
            cboCase.SelectedIndex = -1
            cboContract.SelectedIndex = -1
        End If
    End Sub

    Private Sub btnFindCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindCase.Click
        frmSearchCase.ShowDialog()
    End Sub

    Private Sub btnFindContract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindContract.Click
        frmSearchContract.ShowDialog()
    End Sub

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        inserted()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtDesc.Text) And oc.checknull(txtRevenue.Text) Or oc.checknull(txtDesc.Text) And oc.checknull(txtExpense.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Protected Sub inserted()
        If checknull() Then
            Dim Revenue As Double
            Dim Expense As Double
            Dim isInserted As Boolean = False
            If txtRevenue.Text = "" Then
                Revenue = 0
                If CDbl(txtExpense.Text) > 0 Then
                    Expense = CDbl(txtExpense.Text) * -1
                Else
                    Expense = CDbl(txtExpense.Text)
                End If
            ElseIf txtExpense.Text = "" Then
                Expense = 0
                If CDbl(txtRevenue.Text) < 0 Then
                    Revenue = CDbl(txtRevenue.Text) * -1
                Else
                    Revenue = CDbl(txtRevenue.Text)
                End If
            Else
                If CDbl(txtRevenue.Text) < 0 Then
                    Revenue = CDbl(txtRevenue.Text) * -1
                Else
                    Revenue = CDbl(txtRevenue.Text)
                End If
                If CDbl(txtExpense.Text) > 0 Then
                    Expense = CDbl(txtExpense.Text) * -1
                Else
                    Expense = CDbl(txtExpense.Text)
                End If
            End If
            Dim oCaseIncome As New cCaseIncome
            If rdCase.Checked Then
                If txtRevenue.Text = "" Then
                    If oCaseIncome.insert_CaseIncome(cboCase.SelectedValue, DBNull.Value, Now, txtDesc.Text.Trim, Expense) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                ElseIf txtExpense.Text = "" Then
                    If oCaseIncome.insert_CaseIncome(cboCase.SelectedValue, DBNull.Value, Now, txtDesc.Text.Trim, Revenue) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    If oCaseIncome.insert_CaseIncome(cboCase.SelectedValue, DBNull.Value, Now, txtDesc.Text.Trim, Revenue + Expense) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                End If
            ElseIf rdContract.Checked Then
                If txtRevenue.Text = "" Then
                    If oCaseIncome.insert_CaseIncome(DBNull.Value, cboContract.SelectedValue, Now, txtDesc.Text.Trim, Expense) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                ElseIf txtExpense.Text = "" Then
                    If oCaseIncome.insert_CaseIncome(DBNull.Value, cboContract.SelectedValue, Now, txtDesc.Text.Trim, Revenue) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    If oCaseIncome.insert_CaseIncome(DBNull.Value, cboContract.SelectedValue, Now, txtDesc.Text.Trim, Revenue + Expense) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                End If
            Else
                If txtRevenue.Text = "" Then
                    If oCaseIncome.insert_CaseIncome(DBNull.Value, DBNull.Value, Now, txtDesc.Text.Trim, Expense) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                ElseIf txtExpense.Text = "" Then
                    If oCaseIncome.insert_CaseIncome(DBNull.Value, DBNull.Value, Now, txtDesc.Text.Trim, Revenue) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    If oCaseIncome.insert_CaseIncome(DBNull.Value, DBNull.Value, Now, txtDesc.Text.Trim, Revenue + Expense) Then
                        MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        isInserted = True
                    Else
                        MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                End If
            End If

            If isInserted Then
                reset()
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class